const express = require("express");
const bodyParser = require("body-parser");
// const multer = require("multer");
const cookieParser = require("cookie-parser");
const expressSession = require("express-session");
const uuid = require("uuid");
const cors = require("cors");

let app = express();//⭐
let whiteList = ["/api/login", "/api/reg", "/api/goodslist", "/api/goodsById"];//⭐
app.use(cors());//⭐

// 配置静态资源服务器
app.use(express.static("./../src/"));
// 配置中间件
app.use(expressSession({
    genid: function () {
        return uuid.v1();
    },
    maxAge: 1000 * 60 * 20,
    secret: "1000phone",
    resave: true,
    saveUninitialized: true
}));

app.use(cookieParser("1000phone"));

app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(bodyParser.json());

//⭐
app.use("*", (request, response, next) => {
    if (request.originalUrl == "/favicon.ico") {
        return
    }
    if (request.originalUrl.indexOf("/api") != -1) {
        response.setHeader("Content-type", "application/json;charset=utf-8");
    }
    if (request.session["userInfo"] == undefined || request.session["userInfo"] == null) {
        let isPass = whiteList.some(el => {
            console.log(1111);
            console.log(request.originalUrl.toLowerCase());
            return request.originalUrl.toLowerCase().indexOf(el.toLowerCase()) != -1
        })
        if (isPass) {
            next();
        } else {
            response.json({
                msg: "请先登录",
                status: -1
            })
        };
        console.log(isPass);
    } else {
        next()
    }
})
//⭐
//保安大爷
app.use("/api", require("./routes/apiRoute"));





app.listen(8080, () => {
    console.log("服务器启动完毕！");
})